package com.hex.liyue.giftmodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.adapter.AdapterDiscountGifts;
import com.hex.liyue.giftmodule.bean.DiscountGiftsBean;
import com.hex.liyue.giftmodule.presenter.DiscountGiftsPresenter;
import com.hex.liyue.giftmodule.presenter.IDiscountGiftsView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/20.
 * 折现礼物的界面
 */
public class DiscountGiftsActivity extends BaseActivity implements IDiscountGiftsView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_discount_gifts)
    YRecycleview rvDiscountGifts;
    @Bind(R.id.empty_gift)
    LinearLayout emptyGift;
    private DiscountGiftsPresenter discountGiftsPresenter;
    private int page = 1, page_num = 10;
    private List<DiscountGiftsBean.DataBean> list = new ArrayList<>();
    private AdapterDiscountGifts adapterDiscountGifts;

    @Override
    protected void bindPresenter() {
        discountGiftsPresenter = new DiscountGiftsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        discountGiftsPresenter.getDiscountGifts(page, page_num);

    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_discount_gifts;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        tvToobarTitle.setText("折现的礼物");
        ivToobarBack.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.iv_toobar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }



    @Override
    public void setAdapter(final DiscountGiftsBean discountGiftsBean) {
        if (page == 1) {
            list.clear();
        }
        if (discountGiftsBean != null) {
            if (discountGiftsBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (DiscountGiftsBean.DataBean dataBean : discountGiftsBean.data) {
                list.add(dataBean);
            }
        }
        if (adapterDiscountGifts == null) {
            adapterDiscountGifts = new AdapterDiscountGifts();
            adapterDiscountGifts.setOnItemClickListener(new AdapterDiscountGifts.OnItemClickListener() {
                @Override
                public void onItemClickListener(int position) {
                    String user_gift_cash_id = list.get(position).user_gift_cash_id;
                    Intent intent = new Intent(DiscountGiftsActivity.this, DiscountDetailsActivity.class);
                    intent.putExtra("user_gift_cash_id", user_gift_cash_id);
                    startActivity(intent);
                }
            });
            rvDiscountGifts.setLayoutManager(new LinearLayoutManager(this));
            rvDiscountGifts.setRefreshAndLoadMoreListener(this);
            rvDiscountGifts.setEmptyView(emptyGift);
            rvDiscountGifts.setAdapter(adapterDiscountGifts);
        }
        adapterDiscountGifts.setList(list);
        adapterDiscountGifts.notifyDataSetChanged();
        rvDiscountGifts.reSetStatus();
        L.d("WJ", page + "=size=" + discountGiftsBean.data.size());

    }

    @Override
    public void showLod(String msg) {
        showLoad(DiscountGiftsActivity.this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(DiscountGiftsActivity.this, msg);
    }

    @Override
    public void onRefresh() {
        page = 1;
        discountGiftsPresenter.getDiscountGifts(page, page_num);

    }

    @Override
    public void onLoadMore() {
        page++;
        discountGiftsPresenter.getDiscountGifts(page, page_num);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
